package com.client.job.socket;

import java.util.concurrent.TimeUnit;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @des 服务端未启动，客户端启动断线重连
 * @author mengxiangzhong
 * @date 2021年9月1日
 *
 */
@Slf4j

public class ConnectionListener implements ChannelFutureListener{

	@Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if (!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(new Runnable() {
                @Override
                public void run() {
                    log.error("服务器无法链接，正在尝试重连....");                 
                    ClientByNettyClient.connectWebSocket();
                }
            }, 1L, TimeUnit.SECONDS);
        } else {
            System.out.println("服务端链接成功...");
        }
    }
}